package com.hang.config;

import com.hang.entity.Bug;
import com.hang.entity.Task;
import com.hang.service.IBugService;
import com.hang.service.ITaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务工具，目前该工具提供以下功能
 *
 * 1.定时删除超过14天的已完成的任务
 * 2.定时删除超过14天的已完成的缺陷
 */
@Configuration
@EnableScheduling
@EnableAsync
@Slf4j
public class ScheduledTaskConfig {
    @Resource
    private ITaskService taskService;

    @Resource
    private IBugService bugService;

    @Resource
    private RedisConfig redisConfig;

    @Scheduled(cron = "0 0 5 * * ?")
    @Async
    /**
     * 1.查询解决时间为14天前的已完成的任务列表
     * 2.调用删除接口删除这些数据
     */
    public void deleteFinishedTask(){
        log.info("=======定时删除任务开始======");

        List<Task> taskList = taskService.getFinishTaskIdList();

        deleteTask(taskList);

        log.info("=======定时删除任务结束======");
    }

    @Scheduled(cron = "0 5 5 * * ?")
    @Async
    /**
     * 1.查询解决时间为14天前的已完成的缺陷列表
     * 2.调用删除接口删除这些数据
     */
    public void deleteFinishedBug(){
        log.info("=======定时删除缺陷开始======");

        List<Bug> bugList = bugService.getFinishBugList();

        deleteBug(bugList);

        log.info("=======定时删除缺陷结束======");
    }

    private void deleteBug(List<Bug> bugList) {
        List<Long> bugIdList = bugList.stream().map(Bug::getBugId).distinct().collect(Collectors.toList());

        //只删除mysql中的数据
        bugService.removeByIds(bugIdList);

        //移除redis中相关的任务
        RedisTemplate<String, Object> redisTemplate = redisConfig.getRedisTemplateByDb(2);
        bugList.forEach(
                bug -> {
                    String key = bug.getItemId() + "-" + 3;
                    if (redisTemplate.opsForSet().isMember(key,bug.getBugId())) {
                        redisTemplate.opsForSet().remove(key,bug.getBugId());
                    }
                }
        );

        log.info("本次定时任务成功清除了{}条数据",bugList.size());
    }

    private void deleteTask(List<Task> taskList){
        List<Long> taskIdList = taskList.stream().map(Task::getTaskId).distinct().collect(Collectors.toList());

        //只删除mysql中的数据
        taskService.removeByIds(taskIdList);

        //移除redis中相关的任务
        RedisTemplate<String, Object> redisTemplate = redisConfig.getRedisTemplateByDb(1);
        taskList.forEach(
                task -> {
                    String key = task.getItemId() + "-" + 3;
                    if (redisTemplate.opsForSet().isMember(key,task.getTaskId())) {
                        redisTemplate.opsForSet().remove(key,task.getTaskId());
                    }
                }
        );

        log.info("本次定时任务成功清除了{}条数据",taskList.size());
    }
}
